# -*- coding:utf-8 -*-
"""
@Time : 2024/10/21 21:55 PM
@Author: Xiaomi
@Des: schemas模型
"""
from datetime import datetime
from pydantic import Field, BaseModel, field_validator
from typing import Optional, List, Tuple, Union
from schemas.base import BaseResp, ResAntTable


class CreateAdmin(BaseModel):
    adminname: str = Field(min_length=3, max_length=10)
    password: str = Field(min_length=6, max_length=12)
    admin_phone: Optional[str] = Field(pattern="^1[34567890]\\d{9}$")
    admin_status: Optional[bool]
    remarks: Optional[str]
    roles: Optional[List[int]]


class UpdateAdmin(BaseModel):
    id: int
    adminname: Optional[str] = Field(min_length=3, max_length=10)
    password: Optional[str] = Field(min_length=6, max_length=12)
    admin_phone: Optional[str] = Field(pattern="^1[34567890]\\d{9}$")
    admin_status: Optional[bool]
    remarks: Optional[str]


class SetRole(BaseModel):
    admin_id: int
    roles: Optional[List[int]] = Field(default=[], description="角色")


class AccountLogin(BaseModel):
    adminname: Optional[str] = Field(min_length=3, max_length=10, description="用户名")
    password: Optional[str] = Field(min_length=6, max_length=12, description="密码")
    mobile: Optional[str] = None
    captcha: Optional[str] = None


class ModifyMobile(BaseModel):
    mobile: str = Field(pattern="^1[34567890]\\d{9}$", description="手机号")
    captcha: str = Field(min_length=6, max_length=6, description="6位验证码")


class AdminInfo(BaseModel):
    adminname: str
    admin_type: bool
    nickname: Optional[str]
    admin_phone: Optional[str]
    admin_email: Optional[str]
    full_name: Optional[str]
    scopes: Optional[List[str]]
    admin_status: bool
    header_img: Optional[str]
    sex: int


class AdminListItem(BaseModel):
    key: int
    id: int
    adminname: str
    admin_type: bool
    nickname: Optional[str]
    admin_phone: Optional[str]
    admin_email: Optional[str]
    full_name: Optional[str]
    admin_status: bool
    header_img: Optional[str]
    sex: int
    remarks: Optional[str]
    create_time: datetime
    update_time: datetime


class CurrentAdmin(BaseResp):
    data: AdminInfo


class AccessToken(BaseModel):
    token: Optional[str]
    expires_in: Optional[int]


class AdminLogin(BaseResp):
    data: Union[AccessToken, List]


class AdminListData(ResAntTable):
    data: List[AdminListItem]


class UpdateAdminInfo(BaseModel):
    nickname: Optional[str] = None
    admin_email: Optional[str] = None
    header_img: Optional[str] = None
    admin_phone: Optional[str] = None
    password: Optional[str] = None
    full_name: Optional[str] = None

    @field_validator('*')
    def blank_strings(cls, v):
        if v == "":
            return None
        return v
